package com.yl.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Wcshenqing;
import com.yl.auth.model.entity.Waichushenqingshenpi;
import com.yl.auth.model.vo.WcshenqingVo;
import com.yl.auth.service.WaichushenqingshenpiService;
import com.yl.auth.service.WcshenqingService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
 * <p>
 *  前端控制器
 * </p>
 * @author ln
 * @since 2023-06-19
 */
@Api(tags = "养老护理-外出申请")
@RestController
@RequestMapping("/admin/wcshenqing")
public class WcshenqingController {
    @Resource
    private WcshenqingService wcshenqingService;
    @Resource
    private WaichushenqingshenpiService waichushenqingshenpiService;
    @GetMapping("/getwaichu/{page}/{limit}")
    @ApiOperation(value = "获取全部外出信息")
    public Result getwaichu(@PathVariable Integer page, @PathVariable Integer limit, @ApiParam("老人名称")String lrname, @ApiParam("状态")Integer wczhuangtai, @ApiParam("创建时间")String createtime){
         // 每秒执行一次
        Page<WcshenqingVo> iPage = new Page<>(page,limit);
        Page<WcshenqingVo> pageVo = wcshenqingService.getPageVo(iPage, lrname, wczhuangtai,createtime);
        return Result.ok(pageVo);
    }
    @PostMapping("/addwaichu")
    @ApiOperation(value = "添加外出申请信息")
    public Result save(@RequestBody Wcshenqing wcshenqing){
        wcshenqingService.save(wcshenqing);
        return Result.ok();
    }
    @ApiOperation(value = "根据ID回显")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable @ApiParam("外出申请id")Integer id) {
        WcshenqingVo wcshenqingVo = wcshenqingService.getByIds(id);
        return Result.ok(wcshenqingVo);
    }
    @PutMapping("/updatewaichu")
    @ApiOperation(value = "编辑外出申请信息")
    public Result update(@RequestBody Wcshenqing wcshenqing){
        wcshenqing.setWczhuangtai(3);
        wcshenqingService.updateById(wcshenqing);
        return Result.ok();
    }
    @GetMapping("/getcwlaoren/{id}")
    @ApiOperation(value = "根据Id查询老人和外出信息")
    public Result getcwlaoren(@PathVariable @ApiParam("外出申请id")Integer id){
        WcshenqingVo wcshenqingVo = wcshenqingService.getcwlaoren(id);
        return Result.ok(wcshenqingVo);
    }
    @GetMapping("/getwcgengji/{page}/{limit}")
    @ApiOperation(value = "获取全部外出登记信息，跟外出申请一个表判断他是否审批通过，通过就能查询到外出登记")
    public Result getwcgengji(@PathVariable Integer page, @PathVariable Integer limit, @ApiParam("老人名称")String lrname, @ApiParam("登记时间")String djtime, @ApiParam("登记人")String hname,@ApiParam("登记状态")Integer djzhuangtai){
        Page<WcshenqingVo> iPage = new Page<>(page,limit);
        Page<WcshenqingVo> pageVo = wcshenqingService.getwcgengji(iPage, lrname, djtime,hname,djzhuangtai);
        return Result.ok(pageVo);
    }
    @PutMapping("/updatewcgengji")
    @ApiOperation(value = "编辑延期")
    public Result updatecwgenghuan(@RequestBody  Wcshenqing wcshenqing){
        wcshenqingService.updateById(wcshenqing);
        return Result.ok();
    }
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据Id查询外出申请信息")
    public Result getById(@PathVariable @ApiParam("外出申请id")Integer id){
        WcshenqingVo wcshenqingVo = wcshenqingService.getByIddj(id);
        return Result.ok(wcshenqingVo);
    }
    @GetMapping("/getdjlaoren/{id}")
    @ApiOperation(value = "根据Id查询外出详情")
    public Result getdjlaoren(@PathVariable @ApiParam("外出申请id")Integer id){
        WcshenqingVo wcshenqingVo = wcshenqingService.getdjlaoren(id);
        return Result.ok(wcshenqingVo);
    }
    @GetMapping("/getShenPiLaoRenName/{id}")
    @ApiOperation("根据老人id获取到老人name")
    public Result getShenPiLaoRenName(@ApiParam("老人id")@PathVariable Integer id){
        String name = wcshenqingService.getShenPiLaoRenName(id);
        return Result.ok(name);
    }
    @ApiOperation("根据外出申请的id再获取到该申批信息")
    @GetMapping("/getShenPiWaiChu/{id}")
    public Result getShenPiWaiChu(@ApiParam("外出申请id") @PathVariable Long id){
        Wcshenqing wcshenqing = wcshenqingService.getById(id);
        Integer lrId = wcshenqing.getLrId();
        LambdaQueryWrapper<Waichushenqingshenpi> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Waichushenqingshenpi::getLaorenId,lrId);
        Waichushenqingshenpi waichushenqingshenpi = waichushenqingshenpiService.getOne(lambdaQueryWrapper);
        return Result.ok(waichushenqingshenpi);
    }
    @ApiOperation("编辑后开始流程根据老人id将审批表拒绝信息删除")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@ApiParam("老人id")@PathVariable Integer id){
        LambdaQueryWrapper<Waichushenqingshenpi> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Waichushenqingshenpi::getLaorenId,id);
        waichushenqingshenpiService.remove(lambdaQueryWrapper);
        return Result.ok();
    }
    @ApiOperation("当当前时间大于预计返回时间则修改状态")
    @PutMapping("/updatezhuangtai")
    public Result updatezhuangtai(){
        wcshenqingService.updatezhungtai();
        return  Result.ok();
    }
    @ApiOperation("当当前时间小于预计返回时间则修改状态")
    @PutMapping("/updatezhuangtais")
    public Result updatezhuangtais(){
        wcshenqingService.updatezhungtais();
        return  Result.ok();
    }
}